package com.sba.core.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * 菜单
 * 
 * @author feiliming
 *
 */
@Entity(name = "sys_menu")
public class Menu implements Comparable<Menu>, Serializable {

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
	
	@Column(nullable = false)
	private String name;
	private String href;
	private String icon;
	private int sort;
	
	@Temporal(value = TemporalType.DATE)
	private Date createTime;
	private String target;
	private Long parentId;
	private int isCollapse;
	private int isShow;
	private int isLeaf;
	
	@Transient
	private List<Menu> subMenus = new ArrayList<Menu>();
	
	//双向关联, jackson转json时出现递归循环错误, 在set方法上加@JsonIgnore
	@ManyToMany(mappedBy = "menus", fetch = FetchType.LAZY)
	private Set<Role> roles = new HashSet<Role>();
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getHref() {
		return href;
	}
	public void setHref(String href) {
		this.href = href;
	}
	public String getIcon() {
		return icon;
	}
	public void setIcon(String icon) {
		this.icon = icon;
	}
	public Integer getSort() {
		return sort;
	}
	public void setSort(Integer sort) {
		this.sort = sort;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	public String getTarget() {
		return target;
	}
	public void setTarget(String target) {
		this.target = target;
	}
	public Long getParentId() {
		return parentId;
	}
	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}
	public int getIsCollapse() {
		return isCollapse;
	}
	public void setIsCollapse(int isCollapse) {
		this.isCollapse = isCollapse;
	}
	public int getIsShow() {
		return isShow;
	}
	public void setIsShow(int isShow) {
		this.isShow = isShow;
	}
	public int getIsLeaf() {
		return isLeaf;
	}
	public void setIsLeaf(int isLeaf) {
		this.isLeaf = isLeaf;
	}
	public void setSort(int sort) {
		this.sort = sort;
	}
	public List<Menu> getSubMenus() {
		return subMenus;
	}
	public void setSubMenus(List<Menu> subMenus) {
		this.subMenus = subMenus;
	}
	
	@JsonIgnore
	public Set<Role> getRoles() {
		return roles;
	}
	@JsonIgnore
	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}
	@Override
	public int compareTo(Menu o) {
		if(this.sort > o.getSort())
			return 1;
		if(this.sort < o.getSort())
			return -1;
		return 0;
	}
	
//	@Override
//	public int compareTo(Menu) {
//		int r = 0;
//		Menu menu = (Menu)o;
//		if(menu.getSort() > this.getSort()){
//			return -1;
//		}else{
//			return 1;
//		}
//	}
	
}
